package com.xiaoma.weakreference;

import java.lang.ref.WeakReference;

/**
 * @author xiaomacdut
 * @date 2020年2月15日 下午6:41:36
 */
public class WeakReferenceTest{
    
    public static void main(String [] args){
        Knight knight = new Knight();
        
        System.out.println("knight的内存地址:" + System.identityHashCode(knight));
        
        WeakReference<Knight> reference = new WeakReference<Knight>(knight);
        
        if(reference.get() != null){
            System.out.println("reference 获得knight的内存地址:" + System.identityHashCode(reference.get()));
        }
        
        knight = null;
        System.out.println("knight 置为null 以后reference 获得knight的内存地址:" + System.identityHashCode(reference.get()));
        
        System.out.println("准备gc回收");
        System.gc();
        
        System.out.println("gc回收以后");
        System.out.println("reference 获得knight的内存地址:" + System.identityHashCode(reference.get()));
        
        
    }
    
    /*
    public static void main(String [] args){
        Knight knight = new Knight();
        System.out.println("knight的内存地址:" + System.identityHashCode(knight));
        
        Knight princess = knight;
        System.out.println("princess的内存地址:" + System.identityHashCode(princess));
        
        knight = null;
        
        System.out.println("knight 置为null 以后princess的内存地址:" + System.identityHashCode(princess));
        
        System.out.println("准备gc回收");
        System.gc();
        
        System.out.println("gc回收以后");
        System.out.println("princess的内存地址:" + System.identityHashCode(princess));
        
    }*/
    
    
    static class Knight{}
}
